<script setup>
import axios from "axios";
import {ref} from "vue";

//店铺列表
const tableData = ref([])
const search = ref({})
const shopList = () => {
  axios.post("/api/shop/shopList",search.value).then(res=>{
    if (res.data.code === 200){
      tableData.value = res.data.data;
    }else {
      alert(res.data.msg)
    }
  })
}
shopList()
const select = () => {
  shopList()
}

//获取用户信息
const userInfo = ref({})
axios.get("/api/shop/userInfo").then(res=>{
  if (res.data.code === 200){
    userInfo.value = res.data.data;
  }else {
    alert(res.data.msg)
  }
})

//店铺入驻
const form = ref({})
const addShop = ref(false)
const toAdd = () => {
  addShop.value = ref(true)
}
const add = () => {
  axios.post("/api/shop/addShop",form.value).then(res=>{
    if (res.data.code === 200){
      alert("新增成功")
      location = "/shop"
    }else {
      alert(res.data.msg)
    }
  })
}

//图片上传
const handleAvatarSuccess = (resp) => {
  form.value.shopImg = resp.data
}


//审核通过
const pass = (row) => {
  axios.get("/api/shop/reviewPass/"+row.id).then(res=>{
    if (res.data.code === 200){
      alert("审核完成")
      location.reload()
    }else {
      alert(res.data.msg)
    }
  })
}
//审核驳回
const reject = (row) => {
  axios.get("/api/shop/reviewReject/"+row.id).then(res=>{
    if (res.data.code === 200){
      alert("审核完成")
      location.reload()
    }else {
      alert(res.data.msg)
    }
  })
}

//查看合同
const toContract = () => {
  location = "/contract"
}
</script>

<template>
  店铺名称：<el-input v-model="search.shopName" style="width: 240px" />
  店铺录入时间：<el-date-picker
                  v-model="search.shopStartTime"
                  type="datetime"
              />-
              <el-date-picker
                  v-model="search.shopEndTime"
                  type="datetime"
              />
  <el-button type="primary" @click="select">搜索</el-button>

  <el-button type="primary" @click="toAdd" v-if="userInfo.roleId != 1">店铺入驻</el-button>
  <el-button type="primary" @click="toContract" >查看合同</el-button>
  <el-dialog v-model="addShop">
    <el-form :model="form" label-width="auto" style="max-width: 600px">
      <h2>店铺信息</h2>
      <el-form-item label="店铺名称">
        <el-input v-model="form.shopName" />
      </el-form-item>
      <el-form-item label="店铺简介">
        <el-input v-model="form.introduction" />
      </el-form-item>
      <el-form-item label="店铺运营类别">
        <el-select v-model="form.shopType">
          <el-option label="手机" value="1" />
          <el-option label="电脑" value="2" />
        </el-select>
      </el-form-item>
      <h2>合同信息</h2>
      店铺合同开始时间：<input type="date" v-model="form.startTime" style="width: 240px">
      <br><br>
      店铺合同终止时间：<input type="date" v-model="form.endTime" style="width: 240px"><br><br>
      附件：<el-upload
          class="avatar-uploader"
          action="/api/shop/upload"
          :on-success="handleAvatarSuccess"
          list-type="picture-card"
      >
      </el-upload>
      <el-form-item>
        <el-button type="primary" @click="add">确认</el-button>
        <el-button type="primary" @click="addShop.value == false">取消</el-button>
      </el-form-item>
    </el-form>
  </el-dialog>

  <el-table :data="tableData" style="width: 100%">
    <el-table-column label="编号" width="120">
      <template #default="scope">
        <div style="display: flex; align-items: center">
          <el-icon><timer /></el-icon>
          <span style="margin-left: 10px">{{ scope.row.id }}</span>
        </div>
      </template>
    </el-table-column>
    <el-table-column label="店铺名称" width="180">
      <template #default="scope">
        <div style="display: flex; align-items: center">
          <el-icon><timer /></el-icon>
          <span style="margin-left: 10px">{{ scope.row.shopName }}</span>
        </div>
      </template>
    </el-table-column>
    <el-table-column label="店铺负责人" width="180">
      <template #default="scope">
        <div style="display: flex; align-items: center">
          <el-icon><timer /></el-icon>
          <span style="margin-left: 10px">{{ scope.row.userName }}</span>
        </div>
      </template>
    </el-table-column>
    <el-table-column label="店铺录入时间" width="180">
      <template #default="scope">
        <div style="display: flex; align-items: center">
          <el-icon><timer /></el-icon>
          <span style="margin-left: 10px">{{ scope.row.shopEntryTime }}</span>
        </div>
      </template>
    </el-table-column>
    <el-table-column label="店铺合同开始时间" width="180">
      <template #default="scope">
        <div style="display: flex; align-items: center">
          <el-icon><timer /></el-icon>
          <span style="margin-left: 10px">{{ scope.row.startTime }}</span>
        </div>
      </template>
    </el-table-column>
    <el-table-column label="店铺合同终止时间" width="180">
      <template #default="scope">
        <div style="display: flex; align-items: center">
          <el-icon><timer /></el-icon>
          <span style="margin-left: 10px">{{ scope.row.endTime }}</span>
        </div>
      </template>
    </el-table-column>
    <el-table-column label="店铺合同数量" width="120">
      <template #default="scope">
        <div style="display: flex; align-items: center">
          <el-icon><timer /></el-icon>
          <span style="margin-left: 10px">{{ scope.row.contractQuantity }}</span>
        </div>
      </template>
    </el-table-column>
    <el-table-column label="店铺简介" width="180">
      <template #default="scope">
        <div style="display: flex; align-items: center">
          <el-icon><timer /></el-icon>
          <span style="margin-left: 10px">{{ scope.row.introduction }}</span>
        </div>
      </template>
    </el-table-column>
    <el-table-column label="店铺状态" width="120">
      <template #default="scope">
        <div style="display: flex; align-items: center">
          <el-icon><timer /></el-icon>
          <span style="margin-left: 10px" v-if="scope.row.status == 0">正常</span>
          <span style="margin-left: 10px" v-if="scope.row.status == 1">续约待审核</span>
          <span style="margin-left: 10px" v-if="scope.row.status == 2">已续约</span>
          <span style="margin-left: 10px" v-if="scope.row.status == 3">待续约</span>
        </div>
      </template>
    </el-table-column>
    <el-table-column label="Operations">
      <template #default="scope">
        <el-button size="small" @click="pass(scope.row)" v-if="userInfo.roleId == 1 && scope.row.status == 1">
          审核通过
        </el-button>
        <el-button size="small" @click="reject(scope.row)" v-if="userInfo.roleId == 1 && scope.row.status == 1">
          审核驳回
        </el-button>
      </template>
    </el-table-column>
  </el-table>
</template>

<style scoped>

</style>
